package com.icow.wms.basic.scheduled;

import com.icow.wms.basic.domain.*;
import com.icow.wms.basic.service.IAgvErrorInfoService;
import com.icow.wms.basic.service.IAgvTaskService;
import com.icow.wms.basic.service.ICallbackInterfaceService;
import com.icow.wms.basic.service.ISsxPeripheralService;
import com.icow.wms.basic.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

@Slf4j
@Service
@SuppressWarnings("all")
public class EnterAllFunc {

    @Autowired
    private IAgvTaskService agvTaskService;
    @Autowired
    private ISsxPeripheralService ssxPeripheralService;
    @Autowired
    private IAgvErrorInfoService agvErrorInfoService;
    @Autowired
    private ICallbackInterfaceService callbackInterfaceService;


    public static Set<AgvTask> tasks = new CopyOnWriteArraySet<>();
    public static Set<AgvTask> beginTasks = new CopyOnWriteArraySet<>();
    public static Set<AgvTask> loadFinishTasks = new CopyOnWriteArraySet<>();
    public static Set<AgvTask> unloadFinishTasks = new CopyOnWriteArraySet<>();

    @Scheduled(fixedDelay = 3000)
    public void enterAllFunc() {
        List<Vehicle> vehicles = OpenTcs.getVehicles();
        if (vehicles == null || vehicles.isEmpty()) {
            log.info("没有获取到车辆");
            return;
        }
        vehicles.forEach(vehicle -> {
            String transportOrder = vehicle.getTransportOrder();
            if (transportOrder != null && !transportOrder.startsWith("Park") && !transportOrder.startsWith("Recharge") &&
                    !transportOrder.startsWith("TOrder") && !transportOrder.startsWith("Move")) {
                AgvTask agvTask = new AgvTask();
                agvTask.setInnerTaskName(transportOrder);
                agvTask.setAgvName(vehicle.getName());
                tasks.add(agvTask);
            }
            //监控agv错误信息
            agvErrorInfoService.monitorVehicleErrorInfo(vehicle);
        });
        if (tasks.isEmpty()) {
            return;
        }
        for (AgvTask task : tasks) {
            String orderName = task.getInnerTaskName();
            String agvName = task.getAgvName();
            TransportOrder transportOrder = OpenTcs.getTransportOrder(orderName);
            if (transportOrder == null) {
                log.info(orderName + " 未获取到");
                removeAll(task);
                break;
            }
            if (!beginTasks.contains(task) && OpenTcs.isBegin(transportOrder)) {
                agvTaskService.setOrderState(orderName, OrderState.ORDER_STATE_EXECUTING, agvName);
                callbackInterfaceService.callbackThreadBoot(CallbackMoment.VEHICLE_EXECUTE, agvName, transportOrder);
                beginTasks.add(task);
            } else if (!loadFinishTasks.contains(task) && OpenTcs.isLoadFinish(transportOrder)) {
                ssxPeripheralService.ssxDockThreadBoot(transportOrder, SsxDockMoment.PICK_UP_FINISH);
                agvTaskService.setOrderState(orderName, OrderState.ORDER_STATE_LOAD_FINISH, agvName);
                callbackInterfaceService.callbackThreadBoot(CallbackMoment.PICK_UP_FINISH, agvName, transportOrder);
                loadFinishTasks.add(task);
            } else if (!unloadFinishTasks.contains(task) && OpenTcs.isUnloadFinish(transportOrder)) {
                ssxPeripheralService.ssxDockThreadBoot(transportOrder, SsxDockMoment.PUT_DOWN_FINISH);
                agvTaskService.setOrderState(orderName, OrderState.ORDER_STATE_UNLOAD_FINISH, agvName);
                callbackInterfaceService.callbackThreadBoot(CallbackMoment.PUT_DOWN_FINISH, agvName, transportOrder);
                unloadFinishTasks.add(task);
            } else if (OpenTcs.isFinish(transportOrder)) {
                agvTaskService.setOrderState(orderName, OrderState.ORDER_STATE_FINISH, agvName);
                callbackInterfaceService.callbackThreadBoot(CallbackMoment.TASK_FINISH, agvName, transportOrder);
                removeAll(task);
            } else if (OpenTcs.isCancel(transportOrder)) {
                agvTaskService.setOrderState(orderName, OrderState.ORDER_STATE_FAIL, agvName);
                if (loadFinishTasks.contains(task)) {
                    callbackInterfaceService.callbackThreadBoot(CallbackMoment.PUT_DOWN_FAIL, agvName, transportOrder);
                } else {
                    callbackInterfaceService.callbackThreadBoot(CallbackMoment.PICK_UP_FAIL, agvName, transportOrder);
                }
                callbackInterfaceService.callbackThreadBoot(CallbackMoment.TASK_CANCEL, agvName, transportOrder);
                removeAll(task);
            }
        }
    }

    private void removeAll(AgvTask task) {
        tasks.remove(task);
        beginTasks.remove(task);
        loadFinishTasks.remove(task);
        unloadFinishTasks.remove(task);
    }
}


